package com.material.vtool.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
@EnableCaching
public class RedisCacheConfig extends CachingConfigurerSupport {

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.password}")
    private String password;

    @Value("${spring.redis.port}")
    private int port;

//    @Value("${spring.redis.timeout}")
//    private int timeout;

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
        RedisCacheManager redisCacheManager = RedisCacheManager.builder(connectionFactory).build();
        return redisCacheManager;
    }

    /**
     * @Description: 防止redis入库序列化乱码的问题
     * @return     返回类型
     * @date 2018/4/12 10:54
     */
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){

        RedisSerializer stringSerializer = new StringRedisSerializer();
        RedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();

        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        redisTemplate.setDefaultSerializer(stringSerializer);
        redisTemplate.setEnableDefaultSerializer(true);

        redisTemplate.setKeySerializer(stringSerializer);//key序列化
        redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);  //value序列化
        redisTemplate.setHashKeySerializer(stringSerializer);
        redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    /**
     * @Description: 连接主库用的
     * @return     返回类型
     * @date 20200509
     */
    @Bean
    public RedisTemplate<Object, Object> redisTemplateMaster(){

        LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory();
        connectionFactory.setDatabase(0);
        connectionFactory.setHostName(host);
        connectionFactory.setPassword(password);
        connectionFactory.setPort(port);
        connectionFactory.setTimeout(10000);
        connectionFactory.afterPropertiesSet();

        RedisSerializer stringSerializer = new StringRedisSerializer();
        RedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();

        RedisTemplate<Object, Object> masterRedisTemplate = new RedisTemplate<Object, Object>();
        masterRedisTemplate.setConnectionFactory(connectionFactory);

        masterRedisTemplate.setDefaultSerializer(stringSerializer);
        masterRedisTemplate.setEnableDefaultSerializer(true);

        masterRedisTemplate.setKeySerializer(stringSerializer);//key序列化
        masterRedisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);  //value序列化
        masterRedisTemplate.setHashKeySerializer(stringSerializer);
        masterRedisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);
        masterRedisTemplate.afterPropertiesSet();
        return masterRedisTemplate;
    }

}
